#!/bin/sh
# postinst script for reportlatency
#
# see: dh_installdeb(1)
#
#
# Copyright 2013 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

PKG=reportlatency
VARDIR=/var/lib/$PKG
WWWDIR=$VARDIR/www
DATADIR=$WWWDIR/data
DBDIR=$VARDIR/data
DBFILE=$DBDIR/latency.sqlite3
DBBACK=$DBDIR/backup.sqlite3
DBEXPORT=$DATADIR/latency.sqlite3
PKGSHARE=/usr/share/$PKG
PKGETC=/etc/reportlatency

case "$1" in
    configure)
	if [ ! -f  $DBFILE ] ; then
	  sqlite3 $DBFILE <$PKGSHARE/sqlite3.sql
	  sqlite3 $DBFILE <$PKGSHARE/views-sqlite3.sql
	  sqlite3 $DBFILE <$PKGSHARE/indices-sqlite3.sql
	  for sql in $PKGETC/match.d/*.sql ; do
	      sqlite3 $DBFILE <$sql
	  done
	  chown www-data:www-data $DBFILE
	fi
	chown www-data:www-data $DBDIR
	chown -R www-data:www-data $WWWDIR
	a2enmod expires
	if [ ! -e /etc/apache2/conf.d/reportlatency ] ; then
	  ln -s ../../reportlatency/apache2.conf /etc/apache2/conf.d/reportlatency
	  invoke-rc.d apache2 restart 1>&2
	fi
	if [ ! -e $DBEXPORT ] ; then
	  ln -s $DBBACK $DBEXPORT
	fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
